from urllib import request

import sys


class Mp4Download:
    def __init__(self, downloadUrl):
        self.downloadUrl = downloadUrl

    def progressRatio(self, a, b, c):
        per = 100.0 * a * b / c
        if per > 100:
            per = 1
        sys.stdout.write(" %.2f%% 已经下载的大小：%1d 文件大小：%1d" % (per, a * b, c) + "\r")
        sys.stdout.flush()

    def videoDownload(self, name="video.mp4"):
        request.urlretrieve(url=self.downloadUrl, filename=name, reporthook=self.progressRatio)


if __name__ == "__main__":
    downloadUrl = "https://geega-oa-kbmsuat.oss-cn-chengdu.aliyuncs.com/tce-kbms/kbms-prod/e9c0fcea268841f1925c7ba14ad8c76e_%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%ABEDA.mp4?Expires=1683367194&OSSAccessKeyId=LTAI5tLTEpqcVjPqqrwkSUj5&Signature=3DMip67bW66mwlTfO1PhszbCo7w%3D"
    fileName = "加勒比海盗"
    print("%s下载中" % fileName)
    dl = Mp4Download(downloadUrl)
    dl.videoDownload("C:/Users/e-Xingfu.Yang/Desktop/Tmp/spider/" + fileName + ".mp4")
